package com.pi.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pi.constants.SystemConstants;
import com.pi.domain.ResponseResult;
import com.pi.domain.entity.Article;
import com.pi.domain.entity.Category;
import com.pi.domain.vo.ArticleListVo;
import com.pi.domain.vo.CategoryVo;
import com.pi.domain.vo.PageVo;
import com.pi.mapper.CategoryMapper;
import com.pi.service.ArticleService;
import com.pi.service.CategoryService;
import com.pi.utils.BeanCopyUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 分类表(Category)表服务实现类
 *
 * @author makejava
 * @since 2025-10-13 14:35:24
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;
//    @Autowired
//    private CategoryService categoryService;

    @Override
    public ResponseResult getCategoryList() {
        // 从article中查正式发布（status = 0）的文章，然后去重，留下categoryId
        LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles = articleService.list(queryWrapper);
        Set<Long> artcleIds = articles.stream()
                .map(Article::getCategoryId)
                .collect(Collectors.toSet());

        // 根据categoryId去category中查status为0的数据，返回id和name
        List<Category> categories = listByIds(artcleIds);
        categories = categories.stream()
                .filter(category -> category.getStatus().equals(SystemConstants.STATUS_NORMAL))
                .collect(Collectors.toList());

        // 封装成CategoryVo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);
        return ResponseResult.okResult(categoryVos);

    }




}

